/**
 * jgamebox
 *
 * outlook@China software studio
 * Copyright (c) 2008-2011. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.common.measure;

/**
 * function:
 * @author watson  email: watson.wu@hotmail.com
 * @date 2011-5-30
 */
public enum Direction {
	UP, LEFT, DOWN, RIGHT, FORWARD, BACK;

	public static Position move(Position position, Speed speed, Direction dire) {
		return move(position, speed, dire, 1);
	}

	public static Position move2D(Position position, Speed speed, Direction dire) {
		return move2D(position, speed, dire, 1);
	}

	public static Position move2D(Position position, Speed speed, Direction dire,
			int step) {
		if(step<1) step = 1;
		Position clonePosition = (Position) position.clone();
		if(clonePosition == null) return position;
		switch(dire) {
		case LEFT:
			clonePosition.setX(position.getX() - speed.getX()*step);
			break;
		case RIGHT:
			clonePosition.setX(position.getX() + speed.getX()*step);
			break;
		case UP:
			clonePosition.setY(position.getY() - speed.getY()*step);
			break;
		case DOWN:
			clonePosition.setY(position.getY() + speed.getY()*step);
			break;
		default:
			break;
		}
		return clonePosition;
	}

	public static Position move(Position position, Speed speed, Direction dire,
			int step) {
		if(step<1) step = 1;
		Position clonePosition = (Position) position.clone();
		if(clonePosition == null) return position;
		switch(dire) {
		case LEFT:
			clonePosition.setX(position.getX() - speed.getX()*step);
			break;
		case RIGHT:
			clonePosition.setX(position.getX() + speed.getX()*step);
			break;
		case UP:
			clonePosition.setY(position.getY() - speed.getY()*step);
			break;
		case DOWN:
			clonePosition.setY(position.getY() + speed.getY()*step);
			break;
		case FORWARD:
			clonePosition.setZ(position.getZ() - speed.getZ()*step);
			break;
		case BACK:
			clonePosition.setZ(position.getZ() + speed.getZ()*step);
			break;
		default:
			break;
		}
		return clonePosition;
	}

	public static Position move3D(Position position, Speed speed, Direction[] dires,
			int step) {
		if(step<1) step = 1;
		Position clonePosition = (Position) position.clone();
		if(clonePosition == null) return position;
		for(Direction dire : dires) {
			clonePosition = move(clonePosition, speed, dire, 1);
		}
		return clonePosition;
	}

	public static Direction valueOf(int index) {
		return values()[index];
	}

	@Override
	public String toString() {
		return new StringBuilder().append("Direction[").append(this.name()).append("]").toString();
	}

	public static int getrotateDegree(Direction oldDirection, Direction direction) {
		return (direction.ordinal() - oldDirection.ordinal())*90;
	}
}
